//
//  MTWKWebView.swift
//  BaseComponent
//
//  Created by zhang qiang on 2018/12/3.
//  Copyright © 2018 DDC. All rights reserved.
//

import UIKit
import WebKit

public class MTWKWebView: WKWebView {
    
    public var contentInset: UIEdgeInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0) {
        didSet {
            self.scrollView.contentInset = contentInset
        }
    }
    
    public var allowsBackNavigationGestures: Bool {
        //        get {
        //            return (self.backNavigationGestures?.isEnabled)!
        //        }
        didSet {
            if self.allowsBackNavigationGestures != allowsBackNavigationGestures {
                self.backNavigationGestures?.isEnabled = allowsBackNavigationGestures
            }
        }
    }
    public var allowsForwardNavigationGestures: Bool {
        //        get {
        //            return (self.forwardNavigationGestures?.isEnabled)!
        //        }
        didSet {
            if self.allowsBackForwardNavigationGestures != allowsForwardNavigationGestures {
                self.forwardNavigationGestures?.isEnabled = allowsForwardNavigationGestures
            }
        }
    }
    private var didInit: Bool = false
    private var backNavigationGestures: UIScreenEdgePanGestureRecognizer?
    private var forwardNavigationGestures: UIScreenEdgePanGestureRecognizer?
    private var allowsBackNavigationGesturesSet: Bool = false
    
    
    public override init(frame: CGRect, configuration: WKWebViewConfiguration) {
        self.allowsBackNavigationGestures = false
        self.allowsForwardNavigationGestures = false
        super.init(frame: frame, configuration: configuration)
        self.customIntitialization()
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func customIntitialization() {
        if self.didInit == false {
            self.didInit = true
            self._allowsBackForwardNavigationGestures()
        }
    }
    
    private func _allowsBackForwardNavigationGestures() {
        self.allowsBackNavigationGesturesSet = true
        super.allowsBackForwardNavigationGestures = true
        self.allowsBackNavigationGesturesSet = false
    }
    
    deinit {
        self.backNavigationGestures = nil
        self.forwardNavigationGestures = nil
    }
    
    public override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
        if self.allowsBackNavigationGesturesSet == true && gestureRecognizer.isKind(of: UIScreenEdgePanGestureRecognizer.self) {
            let navigationGestures: UIScreenEdgePanGestureRecognizer = gestureRecognizer as! UIScreenEdgePanGestureRecognizer
            
            if navigationGestures.edges == .left {
                self.backNavigationGestures = navigationGestures
                navigationGestures.isEnabled = (self.backNavigationGestures?.isEnabled)!
                
            }
            if navigationGestures.edges == .right {
                self.forwardNavigationGestures = navigationGestures
                navigationGestures.isEnabled = (self.forwardNavigationGestures?.isEnabled)!
            }
        }
        super.addGestureRecognizer(gestureRecognizer)
    }
    
    
    
    
    
    
    
}
